package com.tjmc.finance.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjmc.finance.service.TransactionRecordsService;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;

import com.tjmc.recruit.common.security.annotation.Inner;
import com.tjmc.recruit.finance.api.entity.TransactionRecordsEntity;
import com.tjmc.recruit.finance.api.enums.WxOrderStatusEnum;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.tjmc.recruit.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 充值记录表
 *
 * @author recruit
 * @date 2024-11-25 14:17:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/transactionRecords" )
@Tag(description = "transactionRecords" , name = "充值记录表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TransactionRecordsController {

    private final TransactionRecordsService transactionRecordsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param transactionRecords 充值记录表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R getTransactionRecordsPage(@ParameterObject Page page, @ParameterObject TransactionRecordsEntity transactionRecords) {
        LambdaQueryWrapper<TransactionRecordsEntity> wrapper = Wrappers.lambdaQuery(transactionRecords);
        return R.ok(transactionRecordsService.page(page, wrapper));
    }


    /**
     * 通过条件查询充值记录表
     * @param transactionRecords 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    public R getDetails(@ParameterObject TransactionRecordsEntity transactionRecords) {
        return R.ok(transactionRecordsService.list(Wrappers.query(transactionRecords)));
    }

    /**
     * 新增充值记录表
     * @param transactionRecords 充值记录表
     * @return R
     */
    @Operation(summary = "新增充值记录表" , description = "新增充值记录表" )
    @SysLog("新增充值记录表" )
    @PostMapping
    public R save(@RequestBody TransactionRecordsEntity transactionRecords) {
        TransactionRecordsEntity orderInfo = transactionRecordsService.generationOrder(transactionRecords);
        boolean save = transactionRecordsService.save(orderInfo);
        if (!save) {
            return R.failed("新增订单失败");
        }
        //微信下单
        transactionRecordsService.wxGenOrder(orderInfo);
        //修改订单状态
        orderInfo.setStatus(WxOrderStatusEnum.NOTPAY.getStatus());
        orderInfo.setStatusDescription(WxOrderStatusEnum.NOTPAY.getStatusDesc());
        transactionRecordsService.updateById(orderInfo);
        //返回支付所需参数
        Map<String, Object> signParam = transactionRecordsService.getSignParam(orderInfo.getPayOrderId());
        if (MapUtil.isEmpty(signParam)) {
            return R.failed("支付参数获取失败");
        }
        return R.ok(signParam, "下单成功");
    }

    @Operation(summary = "通过ID查询订单" , description = "通过ID查询订单" )
    @GetMapping("/{id}" )
    @SysLog("通过ID查询订单")
    public R getById(@PathVariable String id) {
        return R.ok(transactionRecordsService.getById(id));
    }

    /**
     * 导出excel 表格
     * @param transactionRecords 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<TransactionRecordsEntity> exportExcel(TransactionRecordsEntity transactionRecords,String[] ids) {
        return transactionRecordsService.list(Wrappers.lambdaQuery(transactionRecords).in(ArrayUtil.isNotEmpty(ids), TransactionRecordsEntity::getPayOrderId, ids));
    }

    /**
     * 导入excel 表
     * @param transactionRecordsList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    public R importExcel(@RequestExcel List<TransactionRecordsEntity> transactionRecordsList, BindingResult bindingResult) {
        return R.ok(transactionRecordsService.saveBatch(transactionRecordsList));
    }
}
